package com.qf.web.controller;

import com.qf.domain.Stu;
import com.qf.service.StuService;
import com.qf.service.impl.StuServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * projectName:lipengchong
 * author:LiPengChong
 * time:2024/4/30 17:27
 * description:
 */
@WebServlet("/updateStu")
public class UpdateStuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("Text/html;charset=UTF-8");
        //获取前端数据
        String id = request.getParameter("id");
        String stuname = request.getParameter("stuname");
        String stuno = request.getParameter("stuno");
        String pwd = request.getParameter("pwd");
        String idcard = request.getParameter("idcard");
        String startdate = request.getParameter("startdate");
        String status = request.getParameter("status");


        //非空验证

        if (StringUtils.isEmpty(stuname)){
            request.setAttribute("msg","姓名不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        if (StringUtils.isEmpty(stuno)){
            request.setAttribute("msg","学号不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        if (StringUtils.isEmpty(pwd)){
            request.setAttribute("msg","密码不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        if (StringUtils.isEmpty(idcard)){
            request.setAttribute("msg","身份证号不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        if (StringUtils.isEmpty(startdate)){
            request.setAttribute("msg","入学日期不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        if (StringUtils.isEmpty(status)){
            request.setAttribute("msg","权限不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        try {
            //调用业务逻辑
            StuService stuService = new StuServiceImpl();
            Stu stu = stuService.findById(Integer.parseInt(id));
            if (stu!=null){
                stuService.updateStu(stu);
            }
            request.getSession().setAttribute("stu",stu);
            //转发重定向
            request.setAttribute("msg","修改成功");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        } catch (NumberFormatException e) {
           e.printStackTrace();
            request.setAttribute("msg","修改失败"+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


